import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_page/community_business/community_tag_page/subviews/tag_detail_sub_controller.dart';
import 'package:xvideos/hj_page/community_business/community_tag_page/subviews/tag_detail_subview.dart';
import 'package:xvideos/hj_page/community_business/community_tag_page/widget/tag_detail_header.dart';
import 'package:xvideos/hj_page/community_business/widget/community_sort_header.dart';
import 'package:xvideos/hj_page/home_business/widget/collect_status_wrapper.dart';
import 'package:xvideos/hj_utils/screen.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/tools_base/widget/keep_alive_widget.dart';
import 'package:xvideos/tools_base/widget/net_image_widget.dart';
import 'package:provider/provider.dart';
import '../../../tools_base/widget/follow_button.dart';
import 'community_tag_controller.dart';

class UserCenterSortTypeEvent {
  int sortIndex;
  UserCenterSortTypeEvent(this.sortIndex);
}

class CommunityTagDetailPage extends StatefulWidget {
  static const routeName = '/home/tag_detail';

  const CommunityTagDetailPage({super.key});

  @override
  State<CommunityTagDetailPage> createState() => _CommunityTagDetailPageState();
}

class _CommunityTagDetailPageState extends State<CommunityTagDetailPage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: CommunityTagController(),
      global: false,
      builder: (controller) => Scaffold(
        backgroundColor: Color(0xff0E141E),
        extendBodyBehindAppBar: true,
        body: Stack(
          children: [
            ExtendedNestedScrollView(
              controller: controller.scrollCtr,
              pinnedHeaderSliverHeightBuilder: () => AppBar().preferredSize.height + screen.paddingTop,
              headerSliverBuilder: (_, istop) {
                return [
                  SliverAppBar(
                    pinned: true,
                    floating: false,
                    expandedHeight: 64,
                    titleSpacing: 18.w,
                    automaticallyImplyLeading: false,
                    title: _buildAppBarTitle(controller),
                    flexibleSpace: FlexibleSpaceBar(
                      background: SizedBox(),
                    ),
                  )
                ];
              },
              body: Column(
                children: [
                  12.sizeBoxH,
                  CommunitySortHeader(
                    controller: controller.mainTabCtr,
                    sortOnTap: (index) {},
                  ),
                  12.sizeBoxH,
                  Expanded(
                    child: TabBarView(
                      controller: controller.mainTabCtr,
                      children: controller.sorts
                          .map(
                            (e) => TagDetailSubView<TagCOVERController>(tag: controller.model, postSort: e).keepAlive,
                          )
                          .toList(),
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _buildAppBarTitle(CommunityTagController controller) {
    return ChangeNotifierProvider.value(
      value: controller.scrollCtr,
      child: Consumer<ScrollController>(
        builder: (_, ctr, __) {
          print(ctr.offset);
          final isTop = (180 - ctr.offset - kToolbarHeight) == 0;
          return Row(
            children: [
              GestureDetector(
                onTap: () => Get.back(),
                child: Image.asset(
                  isTop ? 'common_back.png'.commonImgPath : 'back_circle.png'.commonImgPath,
                  width: 24,
                  height: 24,
                ),
              ),
              if (isTop) ...[
                4.sizeBoxW,
                NetworkImageLoader(
                  imageUrl: controller.model.coverImg ?? '',
                  width: 30,
                  height: 30,
                  borderRadius: 15,
                ),
                6.sizeBoxW,
                Text(
                  controller.model.name ?? '',
                  style: const TextStyle(
                    fontSize: 14,
                    color: Color(0xffffffff),
                    fontWeight: FontWeight.w500,
                  ),
                ),
                Spacer(),
                CollectStatusWrapper(
                  tagModel: controller.model,
                  builder: () => FollowButton(
                    mediaId: controller.model.id,
                    isFollow: controller.model.hasCollected ?? false,
                    followType: FollowEnum.tag,
                    successsAction: (success) => controller.model.hasCollected = success,
                  ),
                ),
              ] else ...[
                Expanded(
                  child: Center(
                    child: Text(
                      controller.model.name ?? '',
                      style: TextStyle(
                        fontSize: 18,
                        color: Colors.white,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ),
                ),
                (24 + 18.w).sizeBoxH,
              ],
            ],
          );
        },
      ),
    );
  }
}

